package com.bw.module_home.ui

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bw.lib_common.utils.DpSpUtils
import com.bw.lib_mvicore.ui.BaseMviFragment
import com.bw.module_home.R
import com.bw.module_home.databinding.HomeFragmentHomeDatailDescriptionBinding
import com.bw.module_home.model.entity.SimpleVideoEntity

class HomeDetailDescriptionFragment : BaseMviFragment() {
    lateinit var data:SimpleVideoEntity
    lateinit var binding: HomeFragmentHomeDatailDescriptionBinding
    override fun generateViewModel() {
    }

    var agreeFlag:Boolean = false
    var collectFlag:Boolean = false

    override fun initEvent() {
        //点赞
        binding.tvHomeDetailDescriptionAgree.setOnClickListener {
            val currentAgreeNum=binding.tvHomeDetailDescriptionAgree.text

            if(!agreeFlag){
                binding.tvHomeDetailDescriptionAgree.setCompoundDrawables(getLeftDrawable(R.drawable.ic_thumb_up_check),null,null,null)
                binding.tvHomeDetailDescriptionAgree.text=(currentAgreeNum.toString().toInt()+1).toString()
            }else{

                binding.tvHomeDetailDescriptionAgree.setCompoundDrawables(getLeftDrawable(R.drawable.ic_agree),null,null,null)
                binding.tvHomeDetailDescriptionAgree.text=(currentAgreeNum.toString().toInt()-1).toString()
            }
            agreeFlag=!agreeFlag
        }

        //收藏
        binding.tvHomeDetailDescriptionCollect.setOnClickListener {
            if(!collectFlag){
                binding.tvHomeDetailDescriptionCollect.setCompoundDrawables(getLeftDrawable(R.drawable.ic_home_detail_description_collect_check),null,null,null)

            }else{

                binding.tvHomeDetailDescriptionCollect.setCompoundDrawables(getLeftDrawable(R.drawable.ic_home_detail_description_collect),null,null,null)

            }
            collectFlag=!collectFlag
        }

        //分享
        binding.tvHomeDetailDescriptionShared.setOnClickListener {
            showMsg("分享")
        }

        //赞赏
        binding.tvHomeDetailDescriptionAdmire.setOnClickListener {
            ARouter.getInstance().build("/account/admire")
                .withString("name",data.name)
                .withString("title",data.title)
                .withString("authorimg",data.avatar_url)
                .navigation()
        }
    }

    fun getLeftDrawable(id:Int): Drawable {
        val left=resources.getDrawable(id)
        val px2dp = DpSpUtils.dp2px(requireContext(), 30)
        left.setBounds(0,0,px2dp,px2dp)
        return left
    }

    override fun getFragmentRootView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding= HomeFragmentHomeDatailDescriptionBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun lazyLoad() {
        data = arguments?.getParcelable<SimpleVideoEntity>("data") as SimpleVideoEntity
        binding.tvHomeDetailDescriptionAuthor.text=data.name
        val options: RequestOptions = RequestOptions().circleCrop()
        Glide.with(this).load(data.avatar_url).apply(options).into(binding.ivHomeDetailDescriptionAuthorimg)
        binding.tvHomeDetailDescriptionTitle.text=data.title
        parentFragmentManager.beginTransaction().add(R.id.fl_home_detail_Recommend,RecommendVideosFragment()).commitAllowingStateLoss()
    }

    override fun handleState() {
    }
}